package com.wsb.wetchat.service.app.impl;

import com.alibaba.fastjson.JSONObject;
import com.wsb.wetchat.service.app.BaseInfoService;
import com.wsb.wetchat.utils.HttpClientUtils;
import com.wsb.wetchat.utils.RestTemplateUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wang shaobo
 * @date 2022/6/20  14:42
 * 基础信息获取实现类
 */
@Service
public class BaseInfoServiceImpl implements BaseInfoService {


    //获取访问权限码URL
    private final static String ACCESS_TOKEN_URL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";

    //获取企业微信的企业号，根据不同企业更改
    private final static String CORPID = "wwbf92c3a9aa16a6d1";
    //获取企业应用的密钥，根据不同应用更改
    private final static String CORPSECRET = "lelYKLs124CgmDkbCy-AVGi2XPMIuNYKgLDJW8pM9cM";

    private static final Logger logger = LoggerFactory.getLogger(BaseInfoServiceImpl.class);

    @Autowired
    RestTemplateUtils restTemplateUtils;


    @Override
    public String getAccessToken() {

        //访问微信服务器
        String url = ACCESS_TOKEN_URL + "?corpid=" + CORPID + "&corpsecret=" + CORPSECRET;

        try {

            ResponseEntity<Object> objectResponseEntity = restTemplateUtils.get(url, Object.class);
            Object body = objectResponseEntity.getBody();
            System.out.println(body);
            logger.info("请求企业微信Api返回:{}",String.valueOf(body));

            return String.valueOf(body);



        } catch (Exception e) {
            logger.info("调用API获取ACCESS_TOKEN异常:{}", e.getMessage());
            return "调用API获取ACCESS_TOKEN异常";

        }

    }


}
